<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity {

    private $_id;

    public function getId() {
        return $this->_id;
    }

    public function authenticate() {
        $user = Users::model()->findByAttributes(array(
            'login_id' => $this->username,
        ));

        if ($user === null) {
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        } else {
            if ($user->password !== $user->encrypt($this->password)) {
                $this->errorCode = self::ERROR_PASSWORD_INVALID;
            } else { //Authenticate sucessfully
                $this->_id = $user->id;

                $this->setState('login_id', $user->login_id);
                $this->setState('username', $user->full_name);
                $this->setState('is_admin', true);
                $this->setState('is_client', false);

                $this->errorCode = self::ERROR_NONE;
            }
        }

        return $this->errorCode;
    }

}